/**
@title 单个主营行业方法
@param config
	inputBox//主营行业插入框
	url//json路径
@demo
*/

function oneMostlyTrade(config){
	var config = config || {};
	var inputBox = $(config.inputBox || "#inpMostlyTrade"),
		url = config.url || "http://www.shangjia.cn/public/ajax/gbt.html";
	
	$("body").live('click', function(e){
		if(!($(e.target).closest("#MTOaddr")[0] || e.target === inputBox[0])){
			$("#MTOaddr,#MTOaddrIe6").remove()
		};
	});
	
	inputBox.click(function(){
		products(this,url)
	})
	
}

function getProducts(p,code,callback,url){
	var anchors = '';
	$.getJSON(url+ "?parentId="+code, function(r){
		if(callback)callback(r);
		p.html("");
		if(!!r){
			$("#tesst").css({display:"block"});
			$.each(r, function(i, n){
			if(n.name.length > 7 ){
				var realname = n.name.substring(0,6)+"..";
				anchors += "<li><a style='margin:0' href='javascript:;' class='linkage' title=" + n.name + " code='" + n.id + "'>" + realname + "</a></li>"
			}else{
				anchors += "<li><a style='margin:0' href='javascript:;' class='linkage' title=" + n.name + " code='" + n.id + "'>" + n.name + "</a></li>"
			}
			})
			p.append(anchors);
			if ( $.browser.msie && $.browser.version == '6.0' ){
				$("#MTOaddrIe6").css({"height" : $("#MTOaddr").height()+"px"});
			}
		}else{
			
		}
	});
};


function products(obj,url){
	var obj = $(obj);
	var url = url;
	$("#MTOaddr").remove();
	var iptName=obj.attr("id");
	var iptOffSet=obj.offset();
	var iptLeft=iptOffSet.left;
	var iptTop=iptOffSet.top+20;
	var str="<div id='MTOaddr' class='MTaddr'><span>请选择产品分类<a id='MTfh'>返回顶级列表</a><a id='MTgb'>[&nbsp;关闭&nbsp;]</a></span><ul id='tesst'></ul><div style='clear:both;'></div></div>";
	$("body").append(str);
	$("#tesst").css({display:"none"});
	$("#MTOaddr").css({left:iptLeft+"px",top:iptTop+"px"});
	if ( $.browser.msie && $.browser.version == '6.0' ){
		$("body").append("<iframe id='MTOaddrIe6' style='width:400px; height:"+$('#MTOaddr').height()+"px; left:"+iptLeft+"px; top:"+iptTop+"px; z-index:1;position:absolute;'>Hello</iframe>");
	}
	getProducts($("#MTOaddr ul"),'',function(){
		$("#MTOaddr #MTfh").hide();
	},url);
	$("#MTOaddr #MTgb").unbind("click").bind("click",function(){
		$("#MTOaddr, #MTOaddrIe6").remove();
	});
	$("#MTOaddr .linkage").die("click").live("click",function(){
		var $t = $(this);
		getProducts($("#MTOaddr ul"),$t.attr("code"),function(r){
		if (r.length < 1) {
			$("#MTOaddr, #MTOaddrIe6").remove();
			obj.val($t.attr('title'));
			obj.next().val($t.attr("code"));
		}
			$("#MTOaddr #MTfh").show();
		},url);
	});
	$("#MTOaddr #MTfh").die("click").live("click",function(){
		getProducts($("#MTOaddr ul"),'',function(){
			$("#MTOaddr #MTfh").hide();
		},url);
	});
	return false;
};


//通过js路径 插入其他文件路径
$.extend({
	includePath:"",
    include: function(file){
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++){
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
            if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
        }
    },
    includeJsPath: function(file){
		var scripts = document.getElementsByTagName('script');  
		var currentScript = scripts[scripts.length - 1];  
		var currentSrc = currentScript.getAttribute("src");
		var currentSrcLen = currentSrc.lastIndexOf("/");
		var src = currentSrc.substr(0,currentSrcLen+1);
		$.includePath = src;
		$.include(file);
	}
});
$.includeJsPath("images/mostlyTrade.css");